package com.yangyu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @author
 * @version V1.0
 * @Package com.yangyu.config
 * @date 2021/1/4 22:26
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(Environment environment) {
        // 设置要显示swagger的环境
        Profiles of = Profiles.of("default");
        // 判断当前是否处于该环境
        // 通过 enable() 接收此参数判断是否要显示
        boolean show = environment.acceptsProfiles(of);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(show)
                .select()// 通过.select()方法，去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.basePackage("com.yangyu.controller"))
                .build();
    }
    //配置文档信息
    private ApiInfo apiInfo() {

        Contact contact = new Contact("", "http://www.yangyublog.cn", "137481086@qq.com");
        return new ApiInfo(
                "动物保护协会", // 标题
                "一款简洁时尚的博客网站", // 描述
                "v1.0.3", // 版本
                "https://gitee.com/yangyu-java/dashboard/projects", // 组织链接
                contact, // 联系人信息
                "Apach 2.0 许可", // 许可
                "许可链接", // 许可连接
                new ArrayList<>()// 扩展
        );
    }
}
